home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / paul_qureshi / info / voxel_land.txt < prev   
Text File  |  1999-03-27  |  7KB  |  154 lines

  1.  
  2.         Voxel landscapes and How I did it
  3.         ---------------------------------
  4.  
  5.  This document describes the method I used in my demo of a Martian terrain,
  6. which can be found at garbo.uwasa.fi:/pc/demo/mars10.zip.
  7.  It's similar to a floating horizon hidden line removal algorithm, so you'll
  8. find discussion of the salient points in many computer graphics books. The
  9. difference is the vertical line interpolation.
  10.  
  11.  
  12. First, some general points:
  13. ---------------------------
  14.  
  15.  The map is a 256x256 grid of points, each having and 8-bit integer height
  16. and a colour. The map wraps round such that, calling w(u,v) the height at
  17. (u,v), then w(0,0)=w(256,0)=w(0,256)=w(256,256). w(1,1)=w(257,257), etc.
  18.  
  19.  Map co-ords: (u,v) co-ordinates that describe a position on the map. The
  20. map can be thought of as a height function w=f(u,v) sampled discretely.
  21.  
  22.  Screen co-ords: (x,y) co-ordinates for a pixel on the screen.
  23.  
  24.  
  25. To generate the map:
  26. --------------------
  27.  
  28.  This is a recursive subdivision, or plasma, fractal. You start of with
  29. a random height at (0,0) and therefore also at (256,0), (0,256), (256,256).
  30. Call a routine that takes as input the size and position of a square, in the
  31. first case the entire map.
  32.  This routine get the heights from the corners of the square it gets given.
  33. Across each edge (if the map has not been written to at the point halfway
  34. along that edge), it takes the average of the heights of the 2 corners on that
  35. edge, applies some noise proportional to the length of the edge, and writes
  36. the result into the map at a position halfway along the edge. The centre of
  37. the square is the average of the four corners+noise.
  38.  The routine then calls itself recursively, splitting each square into four
  39. quadrants, calling itself for each quadrant until the length of the side is
  40. 2 pixels.
  41.  This is probably old-hat to many people, but the map is made more realistic
  42. by blurring:
  43.  
  44.      w(u,v)=k1*w(u,v)+k2*w(u+3,v-2)+k3*w(u-2,v+4) or something.
  45.  
  46.  Choose k1,k2,k3 such that k1+k2+k3=1. The points at which the map is sampled
  47. for the blurring filter do not really matter - they give different effects,
  48. and you don't need any theoretical reason to choose one lot as long as it
  49. looks good. Of course do everything in fixed point integer arithmetic.
  50.  The colours are done so that the sun is on the horizon to the East:
  51.  
  52.      Colour=A*[ w(u+1,v)-w(u,v) ]+B
  53.  
  54. with A and B chosen so that the full range of the palette is used.
  55.  The sky is a similar fractal but without the colour transformation.
  56.  
  57.  
  58. How to draw each frame
  59. ----------------------
  60.  
  61.  First, draw the sky, and blank off about 50 or so scan lines below the
  62. horizon since the routine may not write to all of them (eg. if you are on top
  63. of a high mountain looking onto a flat plane, the plane will not go to the
  64. horizon).
  65.  Now, down to business. The screen is as follows:
  66.  
  67.      .-------------------------.
  68.      |                         |
  69.      |                         |
  70.      |           Sky           |
  71.      |                         |
  72.      |                         |
  73.      |a------------------------| Horizon
  74.      |                         |
  75.      |                         |    Point (a)=screen co-ords (0,0)
  76.      |          Ground         |     x increases horizontally
  77.      |                         |     y increases downwards
  78.      |                         |
  79.      `-------------------------'
  80.  
  81.  Imagine the viewpoint is at a position (p,q,r) where (p,q) are the (u,v)
  82. map co-ordinates and r is the altitude. Now, for each horizontal (constant v)
  83. line of map from v=r+100 (say) down to v=r, do this:
  84.  
  85.   1. Calculate the y co-ordinate of map co-ord (p,v,0) (perspective transform)
  86.  
  87.   2. Calculate scale factor f which is how many screen pixels high a mountain
  88. of constant height would be if at distance v from q. Therefore, f is small
  89. for map co-ords far away (v>>r) and gets bigger as v comes down towards r.
  90.  
  91.   3. Work out the map u co-ord corresponding to (0,y). v is constant along
  92. each line.
  93.  
  94.   4. Starting at the calculated (u,v), traverse the screen, incrementing the
  95. x co-ordinate and adding on a constant, c, to u such that (u+c,v) are the map
  96. co-ords corresponding to the screen co-ords (1,y). You then have 256 map
  97. co-ords along a line of constant v. Get the height, w, at each map co-ord and
  98. draw a spot at (x,y-w*f) for all x.
  99.  
  100.  Sorry, but that probably doesn't make much sense. Here's an example:
  101. Imagine sometime in the middle of drawing the frame, everything behind a
  102. point (say v=q+50) will have been drawn:
  103.  
  104.      .-------------------------.
  105.      |                         |
  106.      |                         |
  107.      |                         |
  108.      |           ****          |
  109.      |        *********        | <- A mountain half-drawn.
  110.      |-----**************------|
  111.      |*************************|
  112.      |*********       *********|
  113.      |******             ******|
  114.      |.........................| <- The row of dots is at screen co-ord y
  115.      |                         |   corresponding to an altitude of 0 for that
  116.      `-------------------------'   particular distance v.
  117.  
  118.  Now the screen-scanning routine will get called for v=q+50. It draws in a
  119. point for every x corresponding to heights at map positions (u,v) where u
  120. goes from p-something to p+something, v constant. The routine would put points
  121. at these positions: (ignoring what was there before)
  122.  
  123.      .-------------------------.
  124.      |                         |
  125.      |                         |
  126.      |                         |
  127.      |                         |
  128.      |                         |
  129.      |-------------------------|
  130.      |          *****          |
  131.      |       ***     ***       |
  132.      |*******           *******|
  133.      |.........................|
  134.      |                         |
  135.      `-------------------------'
  136.  
  137.  So, you can see that the screen gets drawn from the back, one vertical
  138. section after another. In fact, there's more to it than drawing one pixel
  139. at every x during the scan - you need to draw a vertical line between
  140. (x,y old) to (x,y new), so you have to have a buffer containing the y values
  141. for every x that were calculated in the previous pass. You interpolate
  142. along this line (Gouraud style) from the old colour to the new colour also,
  143. so you have to keep a buffer of the colours done in the last pass.
  144.  Only draw the vertical lines if they are visible (ie. going down,
  145. y new>y old). The screen is drawn from the back so that objects can be drawn
  146. inbetween drawing each vertical section at the appropriate time.
  147.  
  148.  If you need further information or details, mail me or post here... Posting
  149. will allow others to benefit from your points and my replies, though.
  150.  
  151.  Thank you for the response I have received since uploading this program.
  152.  
  153.  Tim Clarke, tjc1005@hermes.cam.ac.uk
  154.